package com.xuge.controller;

import com.xuge.pojo.UserEntry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@RequestMapping("/api/login")
public class LoginUser {

    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/login")
    private String login(@RequestBody UserEntry userEntry){
        //查询 判断 正确获取token
        if (ObjectUtils.isEmpty(userEntry)){
            return "请输入正确的用户名或密码";
        }
        String token = createToken(userEntry.getAge());
        redisTemplate.opsForValue().set(userEntry.getAge(),token);
        return "登录成功";
    }

    private String createToken(String userId){
        return UUID.randomUUID()+String.valueOf(System.currentTimeMillis());
    }

    @PostMapping("/test")
    public String test(){
        return "test";
    }

    @GetMapping("/test2")
    public String test2(){
        return "test2";
    }
}
